#include "welcome_widget.h"
#include "ui_welcome_widget.h"

#include "page1_widget/page1_widget.h"
#include "page2_widget/page2_widget.h"
#include "page3_widget/page3_widget.h"
#include "page4_widget/page4_widget.h"
#include "page5_widget/page5_widget.h"

#include <iostream>


welcome_Widget::welcome_Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::welcome_Widget)
{
    ui->setupUi(this);

    page1_widget = new Page1_Widget(this);
    page2_widget = new Page2_Widget(this);
    page3_widget = new Page3_Widget(this);
    page4_widget = new Page4_Widget(this);
    page5_widget = new Page5_Widget(this);

    base_timer = new QTimer(this);
    connect(base_timer,SIGNAL(timeout()),this,SLOT(basetimer_timeout_handler()));
    base_timer->start(TIMEOUT_BASE_VALUE);

    update_information();


    ui->pb_page_1->click();
}

welcome_Widget::~welcome_Widget()
{
    delete page1_widget;
    delete page2_widget;
    delete page3_widget;
    delete page4_widget;
    delete page5_widget;

    delete base_timer;
    delete ui;
}


//通过定时器不断刷新当前时间
void welcome_Widget::update_current_time()
{
    QDateTime current_time = QDateTime::currentDateTime();
    QString str_time = current_time.toString("yyyy-MM-dd hh:mm:ss dddd");

    ui->label_time->setText(str_time);
}

//定时器超时处理
void welcome_Widget::basetimer_timeout_handler()
{
    static unsigned int time_update_count = 0;

    ++time_update_count;

    if(time_update_count > (TIMEOUT_CURRTIME_UPDATE / TIMEOUT_BASE_VALUE-1))
    {
        time_update_count = 0;
        update_current_time();
    }
}


void welcome_Widget::on_pb_page_1_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->label_title->setText(ui->pb_page_1->text());
}

void welcome_Widget::on_pb_page_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->label_title->setText(ui->pb_page_2->text());
}

void welcome_Widget::on_pb_page_3_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    ui->label_title->setText(ui->pb_page_3->text());
}

void welcome_Widget::on_pb_page_4_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
    ui->label_title->setText(ui->pb_page_4->text());
}

void welcome_Widget::on_pb_page_5_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
    ui->label_title->setText(ui->pb_page_5->text());
}


void welcome_Widget::update_information()
{
    QString info;

    info = COMPANY_INFO;

    ui->label_information->setText(info);
}




